package com.zhiche.lisa.bms.controller.bill;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.dao.model.bill.BillIoDetail;
import com.zhiche.lisa.bms.dao.model.price.PriceIncoming;
import com.zhiche.lisa.bms.service.bill.BillIoDetailService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 客户或承运商账单明细 前端控制器
 * </p>
 *
 * @author WangShuiJin
 * @since 2018-09-06
 */
@RestController
@RequestMapping("/billIoDetail")
@Api(value = "/billIoDetail", description = "客户或承运商账单明细接口", tags = {"客户或承运商账单明细接口"})
public class BillIoDetailController {
    private static final Logger LOGGER = LoggerFactory.getLogger(BillIoDetailController.class);

    @Autowired
    private BillIoDetailService billIoDetailService;

    @PostMapping(value = "/queryBillIoDetailPage")
    @ApiOperation(value = "查询客户或承运商,账单明细列表（分页）", notes = "通过账单编号，得到客户或承运商，账明细列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = BillIoDetail.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<BillIoDetail>> queryBillIoDetailPage(@RequestBody Page<BillIoDetail> page, @RequestParam(required = true) Long billId) {
        RestfulResponse<Page<BillIoDetail>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<BillIoDetail> pageBillIoData = billIoDetailService.queryBillIoDetailPage(page, billId);
            result.setData(pageBillIoData);
        } catch (BaseException e) {
            LOGGER.error("/BillIoDetailController.queryBillIoDetailPage ERROR: " + e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoDetailController.queryBillIoDetailPage ERROR: " + e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getBillIoDetailByHeadId/{headId}")
    @ApiOperation(value = "根据主表id获取明细数据", notes = "根据主表id获取明细数据", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<BillIoDetail>> getBillIoDetailByHeadId(@PathVariable("headId") Long headId) {
        RestfulResponse<List<BillIoDetail>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(billIoDetailService.getBillIoDetailByHeadId(headId));
        } catch (BaseException e) {
            LOGGER.error("BillIoDetailController.getBillIoDetailByHeadId ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("BillIoDetailController.getBillIoDetailByHeadId ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}

